import java.util.Arrays;
import java.util.Scanner;

public class Test {
    //把数组元素翻倍
    /*public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4};
        transformArray(array);
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }
    public static void transformArray(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i]*2;
        }
    }*/


    //把奇数放在偶数之前（下面的方法都是改变数组内容的，如果不想改变数组内容就可以直接进行判断打印）
    /*public static void main(String[] args) {
        int[] array = new int[]{1,21,3,41,5,61};
        //法一：
        //int[] ret = func(array);
        //String ret1 = Arrays.toString(ret);
        //System.out.println(ret1);

        //法二：
        func2(array);
        String ret = Arrays.toString(array);
        System.out.println(ret);

    }
    public static int[] func(int[] array){
        int[] ret = new int[array.length];
        int j = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 == 1) {
                ret[j++] = array[i];
            }
        }
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 == 0) {
                ret[j++] = array[i];
            }
        }
        return ret;
    }*/
    /*public static void func2(int[] array){
        int i = 0;//找偶数
        int j = array.length-1;//找奇数
        while(i < j){
            while(i < j && array[i] % 2 != 0){
                i++;//是奇数就++
            }
            while(i < j && array[j] % 2 == 0){
                j--;//是偶数就--
            }
            //来到这里就已经说明i和j都已经找到了对应的数又或者i==j了
            //当已经满足条件时，即i==j，交换前后结果是一样的，加不加判断条件（if(i!=j)）都一样
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }*/


    //模拟实现二分查找
    /*public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9,10};
        Scanner scanner = new Scanner(System.in);
        int target = scanner.nextInt();
        int ret = searchArray(array, target);
        if(ret == -1){
            System.out.println("找不到："+target);
        }else {
            System.out.println("找到了，在数组中的下标是："+ret);
        }
    }
    public static int searchArray(int[] array, int target){
        int left = 0;
        int right = array.length-1;
        while(left <= right){
            int num = (left + right) / 2;
            if(array[num] < target){
                left = num+1;
            }else if(array[num] > target) {
                right = num-1;
            }else {
                return num;
            }
        }
        return -1;
    }*/


    //在一个数组中找到和为目标值的数所对应的数组下标
    /*public static void main(String[] args) {
        int[] array = new int[]{2,7,11,15};
        Scanner scanner = new Scanner(System.in);
        int target = scanner.nextInt();
        int[] ret =  searchTarget(array, target);
        System.out.println(Arrays.toString(ret));
    }
    public static int[] searchTarget(int[] array, int target){
        int a = -1;
        int b = -1;
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i] + array[j] == target){
                    a = i;
                    b = j;
                }
            }
        }
        int[] ret = new int[]{a,b};
        return ret;
    }*/


    //找出只出现一次的数字（单身狗）
    /*public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,4,3,2,1};
        int ret = searchNum(array);
        System.out.println(ret);
    }
    public static int searchNum(int[] array){
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }*/


    //用摩尔投票法来实现寻找数组中的多数元素
    /*public static void main(String[] args) {
        int[] array = new int[]{2,2,1,1,1,2,2};
        int ret = majorityArray(array);
        System.out.println(ret);
    }
    public static int majorityArray(int[] array){
        int flag = array[0];//候选人
        int count = 1;//计数器
        for (int j = 0; j < array.length; j++) {
            if (array[j] == flag) {
                count++;
            }
            count--;
            if (count == 0) {
                flag = array[j];
                count = 1;
            }
        }
        return flag;
    }*/


    //判断一个数组是否有三个元素连续是奇数
    /*public static void main(String[] args) {
        int[] array1 = new int[]{2,4,6,1};
        int[] array2 = new int[]{1,2,34,3,4,5,7,23,12};
        boolean ret1 = arrayOdd(array1);
        boolean ret2 = arrayOdd(array2);
        System.out.println(ret1);
        System.out.println(ret2);
    }
    public static boolean arrayOdd(int[] array){
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] % 2 == 1){
                count++;
            }else {
                count = 0;
            }
            if(count == 3){
                return true;
            }
        }
        return false;
    }*/
}
